#!/usr/bin/env python

import sys
import re

def sortby(x):
   return int(x.split()[0])

def main():

   if len(sys.argv) >= 3:

      fro_sec = 0.0;
      to_sec = sys.float_info.max;

      if len(sys.argv) == 4:
         fro_sec = float(sys.argv[3]);

      if len(sys.argv) == 5:
         fro_sec = float(sys.argv[3]);
         to_sec = float(sys.argv[4]);


      matches = []
      iterations = []
      totals = []
      maxes = []
      mins = []

      stamp = 0.0;

      f = open(sys.argv[1])

      for line in f:

         if line.startswith('OCC'):
            stamp = int(line.split()[3]) / (1000 * 1000 * 1000);

         if line.startswith('DSC'):

            if (stamp > fro_sec) and (stamp < to_sec):

               #sys.stderr.write('line:\'%s\'' % line);
               #sys.stderr.write('argv[2]:\'%s\'\n' % sys.argv[2]);

               m = re.search(sys.argv[2]+'@0=', line)
               if m:
                  #sys.stderr.write('line:\'%s\'\n' % line)
                  l = (line.split(sys.argv[2])[0]).split()[3]

                  s = re.search(r'@1=[0-9a-f]+', line).group()[3:]
                  #sys.stderr.write('s:\'%s\'\n' % s)

                  #print s;
                  if l not in matches:
                     matches.append(l);
                     iterations.append(1);
                     totals.append(int(s, 16));
                     maxes.append(int(s, 16));
                     mins.append(int(s, 16));
                  else:
                     iterations[matches.index(l)] += 1
                     totals[matches.index(l)] += int(s, 16)
                     if int(s,16) > maxes[matches.index(l)]:
                        maxes[matches.index(l)] = int(s, 16)
                     if int(s,16) < mins[matches.index(l)]:
                        mins[matches.index(l)] = int(s, 16)

      f.close()

      results = []

      i = 0;
      for m in matches:
         results.append ( str(totals[i]) + ' ' + str(iterations[i]) + 'x ' + str(mins[i]) + '..' + str(maxes[i]) + ' ' + m );
         i = i + 1;

      results.sort(key = sortby, reverse=True);

      for r in results:
         print r;

   else:
      print("Usage: %s tdifile pattern <from> <to>" % sys.argv[0])

if __name__ == "__main__":
   main()